#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "systick.h"
#include "wifi_drv.h"
#include "temp_humi_drv.h"
#include "pm25_drv.h"
#include "rtc_drv.h"
#include "delay.h"
#include "wifi_app.h"
#include "RTT_Debug.h"

typedef enum
{
	WIFI_COMM_WAIT,
	WIFI_COMM_OK,
	WIFI_COMM_FAIL,
} WifiCommState_t;

static WifiCommState_t AtCmdHandle(char *cmd, char *rsp, uint32_t timeoutMs)
{
	static WifiCommState_t s_commState = WIFI_COMM_OK;
	static uint64_t s_sendCmdTime;
	char *recvStrBuf;

	if (s_commState != WIFI_COMM_WAIT)
	{
		if (cmd != NULL)
		{
			SendWifiModuleStr(cmd);			
		}
		s_commState = WIFI_COMM_WAIT;
		s_sendCmdTime = GetSysRunTime();
	}
	else
	{
		if ((GetSysRunTime() - s_sendCmdTime) < timeoutMs)
		{
			recvStrBuf = RecvWifiModuleStr();
			if (strstr(recvStrBuf, rsp) != NULL)
			{
				s_commState = WIFI_COMM_OK;
				// ClearRecvWifiStr();  //在业务中去清除,因为授时业务需要解析收到的数据
			}
		}
		else
		{
			s_commState = WIFI_COMM_FAIL;
		}
	}
    return s_commState;	
}

typedef struct {
    /* 要发送的AT命令 */
    char *cmd;
    /* 期望的应答数据，默认处理匹配到该字符串认为命令执行成功 */
    char *rsp;
    /* 得到应答的超时时间，达到超时时间为执行失败，单位ms*/
    uint32_t timeoutMs;
} AtCmdInfo_t;

/*模组初始化命令集*/
static AtCmdInfo_t g_checkModuleCmdTable[] = {
	{
        .cmd = "AT+RST\r\n",        // 软复位
        .rsp = "ready",
		.timeoutMs = 3000,
    },
	{
		.cmd = NULL,                 // 只为等待
		.rsp = "XXXXX",
		.timeoutMs = 1000,
	},
    {
        .cmd = "ATE0\r\n",        // 关闭回显
        .rsp = "OK",
		.timeoutMs = 500,
    },
	{
        .cmd = "AT+CWMODE=1\r\n",
        .rsp = "OK",
		.timeoutMs = 500,	
	},
	{
        .cmd = "AT+CWRECONNCFG=1,0\r\n",  // 设置 Wi-Fi 重连,重连间隔1S,无次数限制
        .rsp = "OK",
		.timeoutMs = 100,	
	},
};

typedef enum 
{
	AT_RST,
	AT_RST_DELAY,
    AT_E0,
	AT_CWMODE_1,
	AT_CWRECONNCFG,
} AtCheckModuleCmdType;

WifiCommState_t CheckWifiModuleWork(void)
{
	WifiCommState_t commState;
	static uint8_t retryCount = 0;
	static AtCheckModuleCmdType cmdType = AT_RST;

	switch (cmdType)
	{
		case AT_RST:
			commState = AtCmdHandle(g_checkModuleCmdTable[AT_RST].cmd, g_checkModuleCmdTable[AT_RST].rsp, 
								 g_checkModuleCmdTable[AT_RST].timeoutMs);
		
			if (commState == WIFI_COMM_OK)
			{
				retryCount = 0;
				ClearRecvWifiStr();
				cmdType = AT_RST_DELAY;
			}
			else if (commState == WIFI_COMM_FAIL)
			{
				retryCount++;
				if (retryCount == 3)
				{
					retryCount = 0;
					return WIFI_COMM_FAIL;
				}
			}
			break;
		
		case AT_RST_DELAY:
			commState = AtCmdHandle(g_checkModuleCmdTable[AT_RST_DELAY].cmd, g_checkModuleCmdTable[AT_RST_DELAY].rsp, 
								 g_checkModuleCmdTable[AT_RST_DELAY].timeoutMs);
		
			if (commState == WIFI_COMM_OK)
			{
				ClearRecvWifiStr();
				cmdType = AT_E0;
			}
			else if (commState == WIFI_COMM_FAIL)
			{
				cmdType = AT_E0;
			}
			break;	
			
		case AT_E0:
			commState = AtCmdHandle(g_checkModuleCmdTable[AT_E0].cmd, g_checkModuleCmdTable[AT_E0].rsp, 
								 g_checkModuleCmdTable[AT_E0].timeoutMs);
		
			if (commState == WIFI_COMM_OK)
			{
				ClearRecvWifiStr();
				retryCount = 0;
				cmdType = AT_CWMODE_1;
			}
			else if (commState == WIFI_COMM_FAIL)
			{
				retryCount++;
				if (retryCount == 3)
				{
					retryCount = 0;
					cmdType = AT_RST;
					return WIFI_COMM_FAIL;
				}

			}
			break;
		case AT_CWMODE_1:
			commState = AtCmdHandle(g_checkModuleCmdTable[AT_CWMODE_1].cmd, g_checkModuleCmdTable[AT_CWMODE_1].rsp, 
								 g_checkModuleCmdTable[AT_CWMODE_1].timeoutMs);
		
			if (commState == WIFI_COMM_OK)
			{
				ClearRecvWifiStr();
				cmdType = AT_CWRECONNCFG;
			}
			else if (commState == WIFI_COMM_FAIL)
			{
				cmdType = AT_RST;
				return WIFI_COMM_FAIL;
			}
			break;
		case AT_CWRECONNCFG:
			commState = AtCmdHandle(g_checkModuleCmdTable[AT_CWRECONNCFG].cmd, g_checkModuleCmdTable[AT_CWRECONNCFG].rsp, 
								 g_checkModuleCmdTable[AT_CWRECONNCFG].timeoutMs);
		
			if (commState == WIFI_COMM_OK)
			{
				ClearRecvWifiStr();
				cmdType = AT_RST;
				return WIFI_COMM_OK;
			}
			else if (commState == WIFI_COMM_FAIL)
			{
				cmdType = AT_RST;
				return WIFI_COMM_FAIL;
			}
			break;
	}
	return WIFI_COMM_WAIT;
}

static AtCmdInfo_t g_checkConnectCmdTable[] = {
	{
        .cmd = "AT+CWSTATE?\r\n",
        .rsp = "CWSTATE:2",
		.timeoutMs = 1000,	
	},
};

typedef enum 
{
	AT_CWSTATE = 0,
} AtcheckConnectCmdType;

WifiCommState_t CheckWifiConnect(void)
{
	WifiCommState_t commState;
	static AtcheckConnectCmdType cmdType = AT_CWSTATE;

	switch (cmdType)
	{
		case AT_CWSTATE:
			commState = AtCmdHandle(g_checkConnectCmdTable[AT_CWSTATE].cmd, g_checkConnectCmdTable[AT_CWSTATE].rsp, 
								 g_checkConnectCmdTable[AT_CWSTATE].timeoutMs);
		
			if (commState == WIFI_COMM_OK)
			{
				ClearRecvWifiStr();
				cmdType = AT_CWSTATE;
				return WIFI_COMM_OK;
			}
			else if (commState == WIFI_COMM_FAIL)
			{
				return WIFI_COMM_FAIL;
			}
			break;
	}
	return WIFI_COMM_WAIT;	
}

const static char g_mqttClientId[] = "k0tp78pCCGv.board1|securemode=2\\,signmethod=hmacsha256\\,timestamp=1717656064029|";  				  
/* 
字符串里\\，第二个\是给at指令用的，第一个\是给编译器用的，不然识别不了第二个\
*/
const static char g_mqttUserName[] = "board1&k0tp78pCCGv";
const static char g_mqttPwd[] = "31fffa629f2e8ecb489ad29f4b6603922a383dbb9a96e11cff1e25757c5cbc21";  // 重新打开网页查看这个数据都会变化，上面那个时间戳也会变化，所以两个都要重新复制过来
const static char g_mqttUrl[] = "iot-06z00c3kqsjgy5d.mqtt.iothub.aliyuncs.com";
								 
static AtCmdInfo_t g_connectMqttCmdTable[] = {
	{
        .cmd = "AT+MQTTUSERCFG=0,1,\"%s\",\"%s\",\"%s\",0,0,\"\"\r\n",
        .rsp = "OK",
		.timeoutMs = 500,	
	},
	{
		.cmd = "AT+MQTTCONN=0,\"%s\",1883,1\r\n",
		.rsp = "OK",
		.timeoutMs = 2000,
	},
	{
		.cmd = "AT+CIPSNTPCFG=1,8,\"cn.ntp.org.cn\",\"ntp.sjtu.edu.cn\"\r\n",
		.rsp = "OK",
		.timeoutMs = 200,
	},
};

typedef enum 
{
	AT_MQTTUSERCFG = 0,
	AT_MQTTCONN,
	AT_SNTPCFG,
} AtConnectMqttCmdType;

WifiCommState_t ConnectMqttServer(void)
{
	WifiCommState_t commState;
	static uint8_t retryCount = 0;
	static AtConnectMqttCmdType cmdType = AT_MQTTUSERCFG;
	char cmdStrBuf[256] = {0};

	switch (cmdType)
	{
		case AT_MQTTUSERCFG:
			sprintf(cmdStrBuf, g_connectMqttCmdTable[AT_MQTTUSERCFG].cmd, g_mqttClientId, g_mqttUserName, g_mqttPwd);
			commState = AtCmdHandle(cmdStrBuf, g_connectMqttCmdTable[AT_MQTTUSERCFG].rsp, 
								 g_connectMqttCmdTable[AT_MQTTUSERCFG].timeoutMs);
			if (commState == WIFI_COMM_OK)
			{
				ClearRecvWifiStr();
				cmdType = AT_MQTTCONN;
			}
			else if (commState == WIFI_COMM_FAIL)
			{
				return WIFI_COMM_FAIL;
			}
			break;		
		case AT_MQTTCONN:
			sprintf(cmdStrBuf, g_connectMqttCmdTable[AT_MQTTCONN].cmd, g_mqttUrl);
			commState = AtCmdHandle(cmdStrBuf, g_connectMqttCmdTable[AT_MQTTCONN].rsp, 
								 g_connectMqttCmdTable[AT_MQTTCONN].timeoutMs);
			if (commState == WIFI_COMM_OK)
			{
				retryCount = 0;
				ClearRecvWifiStr();
				cmdType = AT_SNTPCFG;
			}
			else if (commState == WIFI_COMM_FAIL)
			{
				retryCount++;
				if (retryCount == 3)
				{
					cmdType = AT_MQTTUSERCFG;
					retryCount = 0;
					return WIFI_COMM_FAIL;
				}
			}
			break;
		case AT_SNTPCFG:
			commState = AtCmdHandle(g_connectMqttCmdTable[AT_SNTPCFG].cmd, g_connectMqttCmdTable[AT_SNTPCFG].rsp, 
								 g_connectMqttCmdTable[AT_SNTPCFG].timeoutMs);
			if (commState == WIFI_COMM_OK)
			{
				cmdType = AT_MQTTUSERCFG;
				ClearRecvWifiStr();
				return WIFI_COMM_OK;
			}
			else if (commState == WIFI_COMM_FAIL)
			{
				cmdType = AT_MQTTUSERCFG;
				return WIFI_COMM_FAIL;
			}
			break;		
		default:
			break;
	}
	return WIFI_COMM_WAIT;
}

static void ParaseTime(char *recvStrBuf)
{
	char *weekTab[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
    char *monTab[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
	char weekStr[10], monthStr[10];
	int week, month, day, hour, minute, second, year;
	uint8_t i;
	RtcTime_t rtcTime;
	
	DBG_log("recvStrBuf:%s\n", recvStrBuf); // +CIPSNTPTIME:Wed Jun 5 20:59:43 2024
	
	sscanf(recvStrBuf, "+CIPSNTPTIME:%s %s %d %d:%d:%d %d", weekStr, monthStr, &day, &hour, &minute, &second, &year); 
	
	for (i = 0; i < 7; i++)
	{
		if (strcmp(weekTab[i], weekStr) == 0)
		{
			week = i + 1;
			break;
		}
	}
	for (i = 0; i < 12; i++)
	{
		if (strcmp(monTab[i], monthStr) == 0)
		{
			month = i + 1;
			break;
		}
	}
	DBG_log("ntp time: %d %d %d %d:%d:%d %d\n", week, month, day, hour, minute, second, year);
	if (year == 1970)
	{
		return;
	}
	rtcTime.day = day;
	rtcTime.month = month;
	rtcTime.year = year;
	rtcTime.hour = hour;
	rtcTime.minute = minute;
	rtcTime.second = second;
	SetRtcTime(&rtcTime);
}

const static char g_mqttTopic[] = "/sys/k0tp78pCCGv/board1/thing/event/property/post"; 

static AtCmdInfo_t g_commMqttCmdTable[] = {
	{
        .cmd = "AT+MQTTPUB=0,\"%s\",\"{\\\"params\\\": {\\\"temp\\\": %.1f\\,\\\"humi\\\": %.1f\\,\\\"pm2_5\\\": %d}}\",0,0\r\n",
        /* 特别注意：转义字符\的数量
		
		   发送的AT命令：
		   AT+MQTTPUB=0,"/sys/k0tp7kBek08/board1/thing/event/property/post","{\"params\": {\"temp\": 10.5}}",0,0\r\n
		*/
		.rsp = "OK",
		.timeoutMs = 500,	
	},
	{
		.cmd = "AT+CIPSNTPTIME?\r\n",
		.rsp = "OK",
		.timeoutMs = 500,
	},
};

typedef enum 
{
	AT_MQTTPUB_SENSOR = 0,
	AT_SNTPTIME
} AtCommMqttCmdType;

#define   MQTT_PUB_PERIOD   3000UL    // 上传数据周期，单位ms，不需要太频繁，减少服务器的负担
#define   SNTP_REQ_PERIOD   3000UL    // 授时请求周期，单位ms

WifiCommState_t CommMqttServer(void)
{
	static WifiCommState_t commState = WIFI_COMM_OK;
	static uint8_t retryCount = 0;
	static AtCommMqttCmdType cmdType = AT_MQTTPUB_SENSOR;
	static uint64_t lastPubSysTime = 0;
	static uint64_t lastSntpSysTime = 0;
	char cmdStrBuf[256] = {0};
	char *recvStrBuf;
	float tempData;
	float humiData;
	Pm25SensorData_t pm25SensorData;

	switch (cmdType)
	{
		case AT_MQTTPUB_SENSOR:
			if (commState != WIFI_COMM_WAIT)
			{
				if ((GetSysRunTime() - lastPubSysTime) < MQTT_PUB_PERIOD)
				{
					break;					
				}
				else 
				{
					lastPubSysTime = GetSysRunTime();
				}

			}
			GetTempData(&tempData);
			GetHumiData(&humiData);
			if (!GetPm25Data(&pm25SensorData))
			{
				break;	
			}
			sprintf(cmdStrBuf, g_commMqttCmdTable[AT_MQTTPUB_SENSOR].cmd, g_mqttTopic, tempData, humiData, pm25SensorData.pm2p5Conc);
			commState = AtCmdHandle(cmdStrBuf, g_commMqttCmdTable[AT_MQTTPUB_SENSOR].rsp, 
								 g_commMqttCmdTable[AT_MQTTPUB_SENSOR].timeoutMs);
			if (commState == WIFI_COMM_OK)
			{
				retryCount = 0;
				ClearRecvWifiStr();
				cmdType = AT_SNTPTIME;
			}
			else if (commState == WIFI_COMM_FAIL)
			{
				retryCount++;
				if (retryCount == 3)
				{
					retryCount = 0;
					return WIFI_COMM_FAIL;
				}
			}
			break;		
			
		case AT_SNTPTIME:
			if (commState != WIFI_COMM_WAIT)
			{
				if ((GetSysRunTime() - lastSntpSysTime) < SNTP_REQ_PERIOD)
				{
					break;					
				}
				else 
				{
					lastSntpSysTime = GetSysRunTime();
				}

			}
			commState = AtCmdHandle(g_commMqttCmdTable[AT_SNTPTIME].cmd, g_commMqttCmdTable[AT_SNTPTIME].rsp, 
								 g_commMqttCmdTable[AT_SNTPTIME].timeoutMs);
			if (commState == WIFI_COMM_OK)
			{
				cmdType = AT_MQTTPUB_SENSOR;
				recvStrBuf = RecvWifiModuleStr();
				ParaseTime(recvStrBuf);
				ClearRecvWifiStr();
				return WIFI_COMM_OK;
			}
			else if (commState == WIFI_COMM_FAIL)
			{
				cmdType = AT_MQTTPUB_SENSOR;
				return WIFI_COMM_FAIL;
			}
			break;	
		default:
			break;
	}
	return WIFI_COMM_WAIT;
}

static AtCmdInfo_t g_smartCfgCmdTable[] = {
	{
		.cmd = "AT+CWSTARTSMART\r\n", 
		.rsp = "ssid",               // 因为AT+CWSTARTSMART响应有多个包，驱动使用空闲中断，第一包数据包含热点账号密码 
		.timeoutMs = SMART_CONFIG_RECV_SSID_TIMEOUT,
	},
	{
		.cmd = NULL,                 // 因为AT+CWSTARTSMART响应有多个包，驱动使用空闲中断，第三包数据包含"GOT IP",不发送具体命令，只为解析响应
		.rsp = "GOT IP",
		.timeoutMs = SMART_CONFIG_RECV_GOTIP_TIMEOUT,
	},
	{
		.cmd = NULL,                 // 接收到第三包数据包含"GOT IP",等待一会再去发送STOP命令，不然手机侧提示失败，只为等待
		.rsp = "XXXXX",
		.timeoutMs = SMART_CONFIG_DELAY_TIMEOUT,
	},
	{
        .cmd = "AT+CWSTOPSMART\r\n",
        .rsp = "OK",
		.timeoutMs = 300,	
	},
};

typedef enum 
{
	AT_CWSTARTSMART,
	AT_CWSTARTSMART_RECV,
	AT_CWSTARTSMART_DELAY,
	AT_CWSTOPSMART,
} AtSmartCfgCmdType;


static WifiCommState_t SmartConfigHandle(void)
{
	WifiCommState_t commState;
	static AtSmartCfgCmdType cmdType = AT_CWSTARTSMART;
	//char *recvStrBuf;
	static uint8_t retryCount = 0;
	
	switch (cmdType)
	{
		case AT_CWSTARTSMART:
			commState = AtCmdHandle(g_smartCfgCmdTable[AT_CWSTARTSMART].cmd, g_smartCfgCmdTable[AT_CWSTARTSMART].rsp, 
								 g_smartCfgCmdTable[AT_CWSTARTSMART].timeoutMs);
		
			if (commState == WIFI_COMM_OK)
			{
				//recvStrBuf = RecvWifiModuleStr();	
				ClearRecvWifiStr();				
				cmdType = AT_CWSTARTSMART_RECV;
			}
			else if (commState == WIFI_COMM_FAIL)
			{
				cmdType = AT_CWSTARTSMART_DELAY;
			}
			break;
		case AT_CWSTARTSMART_RECV:
			commState = AtCmdHandle(g_smartCfgCmdTable[AT_CWSTARTSMART_RECV].cmd, g_smartCfgCmdTable[AT_CWSTARTSMART_RECV].rsp, 
								 g_smartCfgCmdTable[AT_CWSTARTSMART_RECV].timeoutMs);
		
			if (commState == WIFI_COMM_OK)
			{
				ClearRecvWifiStr();
				cmdType = AT_CWSTARTSMART_DELAY;
			}
			else if (commState == WIFI_COMM_FAIL)
			{
				cmdType = AT_CWSTARTSMART_DELAY;
			}
			break;
		case AT_CWSTARTSMART_DELAY:
			commState = AtCmdHandle(g_smartCfgCmdTable[AT_CWSTARTSMART_DELAY].cmd, g_smartCfgCmdTable[AT_CWSTARTSMART_DELAY].rsp, 
								 g_smartCfgCmdTable[AT_CWSTARTSMART_DELAY].timeoutMs);
		
			if (commState == WIFI_COMM_OK)
			{
				ClearRecvWifiStr();
				cmdType = AT_CWSTOPSMART;
			}
			else if (commState == WIFI_COMM_FAIL)
			{
				cmdType = AT_CWSTOPSMART;
			}
			break;
		case AT_CWSTOPSMART:
			commState = AtCmdHandle(g_smartCfgCmdTable[AT_CWSTOPSMART].cmd, g_smartCfgCmdTable[AT_CWSTOPSMART].rsp, 
								 g_smartCfgCmdTable[AT_CWSTOPSMART].timeoutMs);
		
			if (commState == WIFI_COMM_OK)
			{
				ClearRecvWifiStr();
				retryCount = 0;
				cmdType = AT_CWSTARTSMART;
				return WIFI_COMM_OK;
			}
			else if (commState == WIFI_COMM_FAIL)
			{
				retryCount++;
				if (retryCount == 3)
				{
					cmdType = AT_CWSTARTSMART;
					retryCount = 0;
					return WIFI_COMM_FAIL;
				}
			}
			break;
	}
	return WIFI_COMM_WAIT;	
}

static bool g_needSmartCfg = false;

void StartSmartCfgWifi(void)
{
	g_needSmartCfg = true;
}

static WifiConnectState_t g_wifiConnectState = WIFI_CHECK_HARDWARE;
WifiConnectState_t GetWifiConnectState(void)
{
	return g_wifiConnectState;
}


typedef enum
{
	CHECK_WIFI_MODULE,
	CHECK_WIFI_CONNECT,
	CONNECT_MQTT_SERVER,
	COMM_MQTT_SERVER,
	SMARTCONFIG_WIFI,
	HWRESET_WIFI_MODULE,
	WIWI_MODULE_ERROR,
} WifiWorkState_t;

void WifiNetworkTask(void)
{
	WifiCommState_t commState;
	static WifiWorkState_t workState = CHECK_WIFI_MODULE;
	static uint8_t hwresetCnt = 0;
	switch (workState)
	{
		case CHECK_WIFI_MODULE:
			g_wifiConnectState = WIFI_CHECK_HARDWARE;
			commState = CheckWifiModuleWork();
			if (commState == WIFI_COMM_OK)
			{
				workState = CHECK_WIFI_CONNECT;
			}
			else if (commState == WIFI_COMM_FAIL)
			{
				workState = HWRESET_WIFI_MODULE;
			}
			break;
		case CHECK_WIFI_CONNECT:
			g_wifiConnectState = WIFI_CONNECT_NWK_ING;
			commState = CheckWifiConnect();
			if (commState != WIFI_COMM_WAIT && g_needSmartCfg)
			{
				workState = SMARTCONFIG_WIFI;
				break;
			}
			if (commState == WIFI_COMM_OK)
			{
				workState = CONNECT_MQTT_SERVER;
			}
			else if (commState == WIFI_COMM_FAIL)
			{
				workState = CHECK_WIFI_CONNECT;
			}
			break;		
		case CONNECT_MQTT_SERVER:
			commState = ConnectMqttServer();
			if (commState == WIFI_COMM_OK)
			{
				workState = COMM_MQTT_SERVER;
			}
			else if (commState == WIFI_COMM_FAIL)
			{
				workState = CHECK_WIFI_MODULE;
			}
			break;
			
		case COMM_MQTT_SERVER:
			commState = CommMqttServer();
			if (commState != WIFI_COMM_WAIT && g_needSmartCfg)
			{
				workState = SMARTCONFIG_WIFI;
				break;
			}
			if (commState == WIFI_COMM_OK)
			{
				g_wifiConnectState = WIFI_CONNECT_MQTT_SUCESS;
				workState = COMM_MQTT_SERVER;
			}
			else if (commState == WIFI_COMM_FAIL)
			{
				g_wifiConnectState = WIFI_CONNECT_MQTT_FAIL;
				workState = CHECK_WIFI_MODULE;
			}
			break;
			
		case SMARTCONFIG_WIFI:
			g_needSmartCfg = false;
			g_wifiConnectState = WIFI_SMART_CONFIGING;
			commState = SmartConfigHandle();
			if (commState == WIFI_COMM_OK)
			{
				workState = CONNECT_MQTT_SERVER;
			}
			else if (commState == WIFI_COMM_FAIL)
			{
				workState = CHECK_WIFI_MODULE;
			}
			break;	
			
		case HWRESET_WIFI_MODULE:
			if (hwresetCnt < 1)                 // 如果AT命令不通，硬件复位1次
			{
				HwresetWifiModule();
				DelayNms(1000);
				workState = CHECK_WIFI_MODULE;
				hwresetCnt++;
			}
			else
			{
				DBG_log("wifi module error!\n");
				workState = WIWI_MODULE_ERROR;  // 如果硬件复位1次，AT命令还是不通，就不再执行WIFI任务的业务逻辑，直接退出，避免影响其他任务
			}
			break;
		default:
			break;
	}
}

